var app = getApp();
import LoadMore from '../../../utils/LoadMore.js'
var load = new LoadMore;
var util = require('../../../utils/util.js');

Page({
  data: {
    url: app.globalData.setting.url,
    resourceUrl: app.globalData.setting.resourceUrl,
    requestData: null,
    tabType: 0,
    goodsCurrentPage: 1,
    timer: null,
  },

  onLoad: function () {
    load.init(this, 'groups', 'requestData');
    this.requestGroupBuy(this.data.tabType);
    this.createTimer();
  },

  onUnload: function () {
    clearInterval(this.data.timer);
  },

  changeTab: function (event) {
    var tabType = event.currentTarget.dataset.type;
    //重置数据
    load.resetConfig();
    this.data.tabType = tabType;
    this.data.requestData = null;
    this.data.goodsCurrentPage = 1;
    this.requestGroupBuy(tabType);
  },

  requestGroupBuy: function (tabType) {
    var that = this;
    var requestUrl = that.data.url + '/api/activity/SalesList';
    if (tabType) {
      requestUrl += '/type/' + tabType;
    }
    requestUrl = requestUrl + '?p=' + that.data.goodsCurrentPage;
    load.request(requestUrl, function () {
      that.data.goodsCurrentPage++;
    });
  },

  onReachBottom: function () {
    if (load.canloadMore()) {
      this.requestGroupBuy(this.data.tabType);
    }
  },

  createTimer: function () {
    var that = this;
    var startTime = (new Date()).getTime();
    this.data.timer = setInterval(function () {
      if (!that.data.requestData || !that.data.requestData.groups) {
        return;
      }
      var remainTime = 0;
      var groups = that.data.requestData.groups;
      for (var i = 0; i < groups.length; i++) {
        var diffTime = startTime - that.data.requestData.server_current_time * 1000;
        groups[i].remainTime = util.remainTime(groups[i].end_time * 1000 - (new Date()).getTime() + diffTime);
      }
      that.setData({ 'requestData.groups': groups });
    }, 1000);
  },

});